<?php
	include_once "config_db.php";
	
	$database = 'mymap';
	
	if (!mysql_connect($db_host, $db_user, $db_pwd))
	die("Can't connect to database");
	
	if (!mysql_select_db($database))
	die("Can't select database");
	
	mysql_query("SET NAMES 'utf8'");
	$function_name = $_REQUEST["function_name"];

	if($function_name == "getLastID") {
		$table_input = $_REQUEST["table_input"];
		getLastID($table_input);
	} elseif($function_name == "getNodeForSegment") {
		$segment_id = $_REQUEST["segment_id"];
		getNodeForSegment($segment_id);
	} elseif($function_name == "insertNodeTo") {
//		echo "tesst";
		insertNodeTo();
	}
	function getLastID($table_input) {
		$result = mysql_query("SELECT id FROM {$table_input}");
		if(mysql_num_rows($result) == 0) {
			$return['id'] = 0;
		} else {
			while($data = mysql_fetch_array($result)) {
				$return['id'] = $data["id"];
			}	
		}
		echo json_encode($return);
	}
	function getNodeForSegment($segment_id) {
		$query = "SELECT lat, lng FROM segment, node ".
				 "WHERE segment.id = ".$segment_id.
				 " AND node.id = segment.start_node";
		$result = mysql_query($query);
		while($data = mysql_fetch_array($result)) {
			$node['lat'] = $data["lat"];
			$node['lng'] = $data["lng"];			
			$return['start_node'] = $node;
		}	
		
		$query = "SELECT lat, lng FROM segment, node ".
				 "WHERE segment.id = ".$segment_id.
				 " AND node.id = segment.end_node";
				 
		$result = mysql_query($query);
		while($data = mysql_fetch_array($result)) {
			$node['lat'] = $data["lat"];
			$node['lng'] = $data["lng"];			
			$return['end_node'] = $node;
		}	
		echo json_encode($return);
	}
	function insertNodeTo() {
		set_time_limit(36000);
		$query = "SELECT id FROM node";
		$result_node = mysql_query($query);
		mysql_data_seek($result_node, 4641);
		$query = "SELECT id, start_node, end_node FROM segment";
		$result_segment = mysql_query($query);

		$i = 0;
		$count = 0;
		while($data_node = mysql_fetch_array($result_node)) {
			$nodeTo = '';
			$isExist = false;
			$node_id = $data_node['id'];			
			while($data_segment = mysql_fetch_array($result_segment)) {
				$segment_id = $data_segment['id'];
				$query = "SELECT nd FROM segment_nd WHERE segment_id = ".$segment_id;
				$result_segment_nd = mysql_query($query);
				$i = 0;
				while($data_segment_nd = mysql_fetch_array($result_segment_nd)) {
					$node_detail_id = $data_segment_nd['nd'];
					if($node_detail_id == $node_id) {
						if($i > 0) {
							mysql_data_seek($result_segment_nd, 0);
							$data_segment_nd = mysql_fetch_array($result_segment_nd);
							$temp = $data_segment_nd['nd'].", "; 
							$nodeTo = $nodeTo.$temp;	
						}
						if($i < (mysql_num_rows($result_segment_nd) - 1)) {
							mysql_data_seek($result_segment_nd, (mysql_num_rows($result_segment_nd) - 1));
							$data_segment_nd = mysql_fetch_array($result_segment_nd);
							$temp = $data_segment_nd['nd'].", "; 
							$nodeTo = $nodeTo.$temp;	
						}		
						$isExist = true;
						break;						
					} 
					$i++;
				}	
			}	
			if($isExist) {
				$nodeTo = substr($nodeTo, 0, strlen($nodeTo) - 2);
				$query = "UPDATE node SET nodeTo = '".$nodeTo."' WHERE id = ".$node_id;
				$result_insert = mysql_query($query);
			} 			
			mysql_data_seek($result_segment, 0);
		}			
		echo "Finish!";
	}
	function find_nodeTo($start_node, $end_node) {
		$nodeTo_id = array();
		$query = "SELECT nodeTo FROM node WHERE id = ".$start_node;
		$result = mysql_query($query);
		while($data = mysql_fetch_array($result)) {
			$nodeTo = process_str($data['nodeTo']);
			$nodeTo_id[$start_node] = $nodeTo;
			for($i = 0; $i < sizeof($nodeTo); $i++) {
				
			}
		}
	}
	
	function process_str($str) {
		$nodeTo_id = array();
		$num_char = substr_count($str, ",");
		for($i = 0; $i < $num_char; $i++) {
			$str_temp = substr($str, 0, strpos($str, ","));
			$str = substr($str, strpos($str, ",") + 2, strlen($str));
			$nodeTo_id[$i] = $str_temp;
		}
		return $nodeTo_id;
	}
?>